let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-empty.png'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      shopId: options.shopId,
      distance: options.distance
    });
    this.getDetail();
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.initData()
    this.shopRevies(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let merchants = this.data.merchants;
    if (merchants.items.length === merchants.total) {
      return;
    }
    this.shopRevies();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 5
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  // 打电话
  phoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone,
    })
  },
  // 去导航
  toLocation() {
    wx.openLocation({
      latitude: parseFloat(this.data.lat),
      longitude: parseFloat(this.data.lng),
      scale: 18
    })
  },
  // 获取商家详情信息
  getDetail() {
    let shopId = this.data.shopId;
    App.HttpService.shopDetail({
      shopId
    }).then(res => {
      if (res.data.code == 200) {
        let data = res.data.data;
        this.setData({
          startTime: data.businessStartTime,
          endTime: data.businessEndTime,
          lng: data.lng,
          lat: data.lat,
          address: data.shopAddress,
          phone: data.shopContactPhone,
          logo: data.shopLogo,
          name: data.shopName,
          count: data.count
        });
      }
    });
  },
  // 商家评价
  shopRevies(cb) {
    let {
      merchants
    } = this.data;
    let shopId = this.data.shopId;
    let params = merchants.params;
    params.shopId = shopId;
    App.HttpService.shopRevies(params).then(res => {
      cb && cb();
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.revieList];
        if (data.revieList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({
          merchants,
          'prompt.hidden': data.revieList.length
        });
      }
    });
  },
  // 跳转到评价详情
  toDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/comments/detail/detail?id=${id}`,
    })
  }
})